package cn.smkt.common.vo;

/**
 * VO:(View Object/Value Object),在当前项目中我们借助VO封装视图层要呈现数据
 */
public class R {
	/**消息表示状态码*/
	private Integer code;
	/**状态码对应的具体信息*/
	private String msg;
	/**数据(基于此属性封装业务层返回的数据)*/
	private Object data;

	/**
	 * 服务于成功
	 */
	public R(){
		this.code = 200;
		this.msg = "请求成功";
	}

	/**
	 * 服务于失败,自定义错误信息
	 * @param message
	 */
	public R(String message){
		this.code=500;
		this.msg =message;

	}
	/**
	 * 服务于失败 异常信息
 	 */
	public R(Throwable e){
		this.code=500;
		this.msg =e.getMessage();
	}

	/**
	 * 服务于成功,携带数据
	 * @param message
	 * @param data
	 */
	public R(String message, Object data){
		this.code=200;
		this.data = data ;
		this.msg = message;
	}

	/**
	 * 服务于失败,自定义码
	 * @param code
	 * @param message
	 */
	public R(Integer code, String message){
		this.code=code;
		this.msg =message;
	}



	public static R ok(){
		return new R();
	}

	public static R ok(Object o){
		return new R("请求成功",o);
	}

	public static R error(String message){
		return new R(message);
	}

	public static R error(Integer code, String message){
		return new R(code,message);
	}

	@Override
	public String toString() {
		return "JsonResult{" +
				"code=" + code +
				", message='" + msg + '\'' +
				", data=" + data +
				'}';
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}
}
